package com.kamistoat.meimeimall.meimeimallproduct.config;

import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.session.web.http.CookieSerializer;
import org.springframework.session.web.http.DefaultCookieSerializer;

@Configuration
public class ProductSessionConfig {

    /**
     * 想要自定义cookie，就需要自定义Bean
     *
     * @return
     */
    @Bean
    public CookieSerializer cookieSerializer() {
        // 直接在默认的CookieSerializer上进行自定义
        DefaultCookieSerializer defaultCookieSerializer = new DefaultCookieSerializer();
        // 1.cookie的过期时间。默认是只要游览器不关闭就不会过期。就不改了
        // defaultCookieSerializer.setCookieMaxAge();
        // 2.cookie的Domain
        // 直接使用.setDomainName()明确指定新的Domain，也可以使用.setDomainNamePattern()以正则化表达式指定
        defaultCookieSerializer.setDomainName("meimeimall.com");
        // 3.cookie的名字，之前默认是叫session
        defaultCookieSerializer.setCookieName("LOGINSESSION");
        return defaultCookieSerializer;
    }

    /**
     * 想要自定义序列化机制，同样需要自定义Bean
     */
    @Bean
    public RedisSerializer<Object> springSessionDefaultRedisSerializer() {
        // ParserConfig.getGlobalInstance().addAccept("org.springframework.web.servlet.");
        return new GenericFastJsonRedisSerializer();
    }
}
